package com.yaosheng.thread.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by yaosheng on 2021/6/30.
 */
public class ReentrantLock3 extends Thread{

    private static ReentrantLock lock = new ReentrantLock (true); // 参数设置为true，表示为公平锁,锁执行遵行先来先执行的原则

    public void run(){
        for(int i=0;i<100;i++){
            lock.lock ();
            try{
                System.out.println (Thread.currentThread ().getName () + "获得锁");
            }finally {
                lock.unlock ();
            }
        }
    }

    public static void main(String[] args) {

        ReentrantLock3 rl = new ReentrantLock3 ();
        Thread t1 = new Thread (rl);
        Thread t2 = new Thread (rl);
        t1.start ();
        t2.start ();
    }
}
